package com.cn.cash.baselib;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.module.AppGlideModule;
import com.cn.cash.baselib.util.CNUtil;

import java.io.File;

/**
 * Created by CN on 2018-1-10.
 */

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        builder.setDiskCache(new DiskCache.Factory() {
            @Override
            public DiskCache build() {
                CNUtil.log("====调用了MyAppGlideModule====");
                File cacheLocation = BaseManager.getImgCacheFile() == null ? getDiskCachePath(BaseManager.getApplication()) : BaseManager.getImgCacheFile();
                return DiskLruCacheWrapper.create(cacheLocation, 1024 * 1024 * 1024);
            }
        });
    }

    public static File getDiskCachePath(Context context) {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            return context.getExternalCacheDir();
        } else {
            return context.getCacheDir();
        }
    }
}